/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.Comptencia;

import Controlador.ControladorCompetencia;
import Controlador.ControladorDisciplina;
import Controlador.ControladorEspacioDeportivo;
import Logica.Competencia;
import Logica.Disciplina;
import Logica.EspacioDeportivo;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.JOptionPane;

/**
 *
 * @author Invitado
 */
public class PanelEliminarCompetencia extends javax.swing.JPanel {

    /**
     * Creates new form PanelEliminarCompetencia
     */
    ControladorCompetencia controladorCompetencia;
    ArrayList<String> idsEspacios;
    ArrayList<String> idsDisciplinas;
    ArrayList<String> idsCompetencia;
    
    public PanelEliminarCompetencia() {
        initComponents();
        this.cargarDatosCB();
        this.jPanelDatos.setVisible(false);
    }

    private void cargarDatosCB()
    {
        this.jCBDisciplina.removeAllItems();
        this.jCBEspacioDeportivo.removeAllItems();
        this.jCBCompetencia.removeAllItems();
        
        ControladorDisciplina controlDisciplina = new ControladorDisciplina();
        ControladorEspacioDeportivo controlEspacioDep = new ControladorEspacioDeportivo();
        
        Vector<Disciplina> disciplinas = (Vector<Disciplina>) controlDisciplina.consultarDisciplinas();
        Vector<EspacioDeportivo> espacios = (Vector<EspacioDeportivo>) controlEspacioDep.consultarEspaciosDeportivos();
        Vector<Competencia> competencias = (Vector<Competencia>) this.controladorCompetencia.consultarCompetencias();
        
        for(int i=0; i<disciplinas.size(); i++)
        {
            Disciplina disciplina = disciplinas.get(i);
            this.idsDisciplinas.add(disciplina.getIdDisciplina());
            this.jCBDisciplina.addItem(disciplina.getNombre());
        }
        
        for(int i=0; i<espacios.size(); i++)
        {
            EspacioDeportivo espacio = espacios.get(i);
            this.idsEspacios.add(espacio.getIdEspacio());
            this.jCBEspacioDeportivo.addItem(espacio.getNombre()+" - "+espacio.getDireccion());
        }
        
        for(int i=0; i<competencias.size(); i++)
        {
            Competencia competencia = competencias.get(i);
            this.idsCompetencia.add(competencia.getIdCompetencia());
            this.jCBCompetencia.addItem(competencia.getNombre()+" - "+competencia.getIdDisciplina().getNombre()+"("+competencia.getFecha()+")");
            
        }
        
    }
    
    private void cargarDatosCompetencia(Competencia competencia)
    {
        this.jTFNombre.setText(competencia.getNombre());
        this.jTADescripcion.setText(competencia.getDescripcion());
        this.jDateFecha.setDate(competencia.getFecha());
        
        this.jCBDisciplina.setSelectedIndex(this.idsDisciplinas.indexOf(competencia.getIdDisciplina().getIdDisciplina()));
        this.jCBEspacioDeportivo.setSelectedIndex(this.idsEspacios.indexOf(competencia.getIdEspacio().getIdEspacio()));
        
    }
    
    private void limpiarDatos()
    {
        this.jTFNombre.setText("");
        this.jTADescripcion.setText("");
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jCBCompetencia = new javax.swing.JComboBox();
        jPanelDatos = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jTFNombre = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTADescripcion = new javax.swing.JTextArea();
        jLabel14 = new javax.swing.JLabel();
        jDateFecha = new com.toedter.calendar.JDateChooser();
        jLabel12 = new javax.swing.JLabel();
        jCBEspacioDeportivo = new javax.swing.JComboBox();
        jLabel13 = new javax.swing.JLabel();
        jCBDisciplina = new javax.swing.JComboBox();
        jBEliminar = new javax.swing.JButton();
        jBCancelar = new javax.swing.JButton();

        jLabel1.setText("Competencia");

        jCBCompetencia.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jCBCompetencia.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCBCompetenciaActionPerformed(evt);
            }
        });

        jLabel2.setText("Nombre");

        jLabel3.setText("Descripcion");

        jTADescripcion.setColumns(20);
        jTADescripcion.setRows(5);
        jScrollPane1.setViewportView(jTADescripcion);

        jLabel14.setText("Fecha");

        jLabel12.setText("Espacio Deportivo");

        jLabel13.setText("Disciplina");

        jBEliminar.setText("Eliminar");
        jBEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBEliminarActionPerformed(evt);
            }
        });

        jBCancelar.setText("Cancelar");

        javax.swing.GroupLayout jPanelDatosLayout = new javax.swing.GroupLayout(jPanelDatos);
        jPanelDatos.setLayout(jPanelDatosLayout);
        jPanelDatosLayout.setHorizontalGroup(
            jPanelDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelDatosLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelDatosLayout.createSequentialGroup()
                        .addGroup(jPanelDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)
                            .addComponent(jLabel14))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanelDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jDateFecha, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTFNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 288, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanelDatosLayout.createSequentialGroup()
                        .addGroup(jPanelDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel12)
                            .addComponent(jLabel13))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanelDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jCBDisciplina, javax.swing.GroupLayout.PREFERRED_SIZE, 272, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jCBEspacioDeportivo, javax.swing.GroupLayout.PREFERRED_SIZE, 272, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanelDatosLayout.createSequentialGroup()
                                .addComponent(jBEliminar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jBCancelar)))))
                .addContainerGap(121, Short.MAX_VALUE))
        );
        jPanelDatosLayout.setVerticalGroup(
            jPanelDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelDatosLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTFNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanelDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addGroup(jPanelDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel14)
                    .addComponent(jDateFecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanelDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12)
                    .addComponent(jCBEspacioDeportivo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanelDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel13)
                    .addComponent(jCBDisciplina, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanelDatosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBEliminar)
                    .addComponent(jBCancelar))
                .addContainerGap(37, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanelDatos, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jCBCompetencia, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jCBCompetencia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelDatos, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jCBCompetenciaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCBCompetenciaActionPerformed
        // TODO add your handling code here:
        this.jCBCompetencia.setEnabled(false);
        int indice = this.jCBCompetencia.getSelectedIndex();
        Competencia competencia = this.controladorCompetencia.consultarCompetencia(this.idsCompetencia.get(indice));
        this.cargarDatosCompetencia(competencia);
        this.jPanelDatos.setVisible(true);
    }//GEN-LAST:event_jCBCompetenciaActionPerformed

    private void jBEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBEliminarActionPerformed
        // TODO add your handling code here:
        try
        {
            String idCompetencia;

            idCompetencia = this.idsCompetencia.get(this.jCBCompetencia.getSelectedIndex());

            boolean exito = this.controladorCompetencia.eliminarCompetencia(idCompetencia);
            if(exito)
            {
                JOptionPane.showMessageDialog(this, "La Operación se Realizo con Exito", "Exito", JOptionPane.INFORMATION_MESSAGE);
            }
            else
            {
                JOptionPane.showMessageDialog(this, "Ocurrio un error, intentelo de nuevo", "Error", JOptionPane.ERROR_MESSAGE);
            }

        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(this, "El valor de algunos datos son invalidos", "Error de tipo", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jBEliminarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBCancelar;
    private javax.swing.JButton jBEliminar;
    private javax.swing.JComboBox jCBCompetencia;
    private javax.swing.JComboBox jCBDisciplina;
    private javax.swing.JComboBox jCBEspacioDeportivo;
    private com.toedter.calendar.JDateChooser jDateFecha;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanelDatos;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTADescripcion;
    private javax.swing.JTextField jTFNombre;
    // End of variables declaration//GEN-END:variables
}
